using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{

    class CLSMAETARP : ObservableObject
    {

MDOgeneral mod = new MDOgeneral();

#region Metodos basicos tabla

    public int guardar(List<clases.CLSMAETARP> LisPre)
    {

        try
        {
            MDOgeneral.transaccion = MDOgeneral.conexion.BeginTransaction();

            for (int i = 0; i <= LisPre.Count - 1; i++)
            {

                if (LisPre[i].existe())
                {
                    LisPre[i].actualizar();
                }
                else
                {
                    LisPre[i].insertar();
                }
            }
            MDOgeneral.transaccion.Commit();
        }
        catch (Exception ex)
        {

            MDOgeneral.transaccion.Rollback();
            throw (ex);
        }
        return 1;
    }
    public int insertar()
    {
        try
        {
            string query = "insert into MAETARP (CODLIS_TAR,CODREF_TAR,CODCOL_TAR,CODTAL_TAR,VALOR_TAR) values ('"+CODLIS_TAR+"','"+CODREF_TAR+"','"+CODCOL_TAR+"','"+CODTAL_TAR+"',"+VALOR_TAR+")";
            return mod.comando(query);
        }
        catch (Exception e) { throw e; }
    }
    public int actualizar()
    {
        try
        {
            string query = "update MAETARP set VALOR_TAR=" + VALOR_TAR + " where CODREF_TAR='" + CODREF_TAR + "' and CODLIS_TAR='" + CODLIS_TAR + "' and CODCOL_TAR='" + CODCOL_TAR + "' and CODTAL_TAR='" + CODTAL_TAR + "'";
            return mod.comando(query);
        }
        catch (Exception e) { throw e; }
    }
    public Boolean existe()
    {
        try
        {
            string query = "select count(*) from MAETARP where CODREF_TAR='"+CODREF_TAR+"' and CODLIS_TAR='"+CODLIS_TAR+"' and CODCOL_TAR='"+CODCOL_TAR+"' and CODTAL_TAR='"+CODTAL_TAR+"'";
            DataTable val = mod.consulta(query);
            if (val.Rows[0][0].ToString().Equals("0"))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        catch (Exception e) { throw e; }
    }
    public DataTable consPreRef() {
        try {
            //string query = "select CODREF_PRO,CODCOL_PRO,(select NOMCOL_COL from maecol where CODCOL_COL=CODCOL_PRO) as NOMCOL,CODTAL_PRO,VALOR_TAR,CODLIS_PRE,NOMLIS_PRE,TIPLIS_PRE from LISPREP full outer join (select CODREF_PRO,CODCOL_PRO,CODTAL_PRO,CODLIS_TAR,VALOR_TAR from MAETARP right outer join maepro on CODREF_TAR=CODREF_PRO and CODCOL_TAR=CODCOL_PRO and CODTAL_TAR=CODTAL_PRO where CODREF_PRO='" + CODREF_TAR + "') on CODLIS_PRE=CODLIS_TAR where (APLICAA_PRE='P' OR APLICAA_PRE is null) Order by TIPLIS_PRE desc, CODREF_PRO, CODCOL_PRO, CODTAL_PRO";
            string query = "select CODREF_PRO,CODCOL_PRO,(select NOMCOL_COL from maecol where CODCOL_COL=CODCOL_PRO) as NOMCOL,CODTAL_PRO,VALOR_TAR,CODLIS_PRE,NOMLIS_PRE,TIPLIS_PRE from LISPREP full outer join (select CODREF_PRO,CODCOL_PRO,CODTAL_PRO,CODLIS_TAR,VALOR_TAR from MAETARP right outer join maepro on CODREF_TAR=CODREF_PRO and CODCOL_TAR=CODCOL_PRO and CODTAL_TAR=CODTAL_PRO where CODREF_PRO='" + CODREF_TAR + "') on CODLIS_PRE=CODLIS_TAR where (APLICAA_PRE='P' OR APLICAA_PRE is null) Order by CODLIS_PRE";
            DataTable val = mod.consulta(query);
            if (val.Rows.Count > 0)
            {
                EXISTE = true;
                CAMBIO = false;
                return val;
            }
            else
            {
                EXISTE = false;
            }
            return val;
        }
        catch (Exception ex) { throw (ex); }
}
    public void consTarUniRef() {
        try {
         
            string query = "select distinct coalesce(valor_tar,0) as valor_tar from maetarp where codlis_tar='" + CODLIS_TAR + "' and codref_tar='" + CODREF_TAR + "' and codcol_tar='" + CODCOL_TAR + "'";
            DataTable val = mod.consulta(query);
            if (val.Rows.Count > 0)
            {

                VALOR_TAR = Convert.ToInt32(mod.consulta(query).Rows[0]["valor_tar"]);
            }
            

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

#endregion

    #region  Propiedades tabla MAETARP

    private string _CODLIS_TAR;
    public string CODLIS_TAR
    {
        get { return _CODLIS_TAR; }
        set
        {  _CODLIS_TAR = value;
            RaisePropertyChangedEvent("CODLIS_TAR");
        }
    }

    private string _CODREF_TAR;
    public string CODREF_TAR
    {
        get { return _CODREF_TAR; }
        set
        {
            _CODREF_TAR = value;
            RaisePropertyChangedEvent("CODREF_TAR");
        }
    }

    private string _CODCOL_TAR;
    public string CODCOL_TAR
    {
        get { return _CODCOL_TAR; }
        set
        {
            _CODCOL_TAR = value;
            RaisePropertyChangedEvent("CODCOL_TAR");
        }
    }

    private string _CODTAL_TAR;
    public string CODTAL_TAR
    {
        get { return _CODTAL_TAR; }
        set
        {
            _CODTAL_TAR = value;
            RaisePropertyChangedEvent("CODTAL_TAR");
        }
    }
  
    private double _VALOR_TAR;
    public double VALOR_TAR
    {
        get { return _VALOR_TAR; }
        set
        {
            _VALOR_TAR = value;
            RaisePropertyChangedEvent("VALOR_TAR");
        }
    }

    private Boolean _CAMBIO=false;
    public Boolean CAMBIO
    {
        get { return _CAMBIO; }
        set
        {
            _CAMBIO = value;
            RaisePropertyChangedEvent("CAMBIO");
        }
    }
    
    private Boolean _EXISTE=false;
    public Boolean EXISTE
    {
        get { return _EXISTE; }
        set
        {
            _EXISTE = value;
            RaisePropertyChangedEvent("EXISTE");
        }
    }

#endregion

      }
}
